<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Sponsor extends SMB_Controller {
	
	public function __construct() {
		parent::__construct();
		$this->load->library('sponsorship');
	}
	
	public function index() {
		$this->_get_userinfo();
		
		$this->load->helper('ext_form');
		$this->load->helper('table');
		
		$this->_load_common_header();
		$this->_load_common_navigation();
		$this->_load_common_sidebar(get_user_sidebar($this->userinfo['role'], 'sponsor'));
		$params = array(
			'role' => $this->userinfo['role'],
			'addstatus' => $this->session->flashdata('sponsoradd'),
			'editstatus' => $this->session->flashdata('sponsoredit')
		);
		
		if ($this->input->get('search') == FALSE) {
			$params['sponsors'] = $this->sponsorship->get_sponsors();
			$params['title'] = 'Daftar Semua Sponsor';
			$params['empty'] = 'Belum ada sponsor ditambahkan';
		} else {
			$name = $this->input->get('name', TRUE);
			$params['sponsors'] = $this->sponsorship->get_sponsors($name);
			$params['title'] = 'Daftar Hasil Pencarian Sponsor';
			$params['empty'] = 'Tidak ada sponsor yang sesuai dengan kriteria pencarian.';
		}
		
		$params['pager'] = get_pager(5);
		$this->load->view('common/sponsor_list', $params);
		$this->_load_common_footer();
	}
	
	public function edit($id = NULL) {
		$this->_get_userinfo();
		if ($id == NULL) {
			redirect('mahalum/sponsor/index', 'location');
			exit;
		}
		$sponsor = $this->sponsorship->get_sponsor_info_by_id($id);
		if ($sponsor == NULL) {
			redirect('mahalum/sponsor/index', 'location');
			exit;
		}
		
		$this->form_validation->set_error_delimiters('<span class="error">', '</span>');
		if ($this->form_validation->run('edit_sponsor_profile') == FALSE) {
			$this->_load_common_header();
			$this->_load_common_navigation();
			$this->_load_common_sidebar(get_user_sidebar($this->userinfo['role'], 'sponsor'));
			$params = array(
				'role' => $this->userinfo['role'],
				'sponsor' => $sponsor,
				'editstatus' => ''
			);
			$this->load->view('common/sponsor_edit', $params);
			$this->_load_common_footer();
		} else {
			$id= $this->input->post('id', TRUE);
			$name = $this->input->post('name', TRUE);
			$address = $this->input->post('address', TRUE);
			$contactName = $this->input->post('contactName', TRUE);
			$contactNo = $this->input->post('contactNo', TRUE);
			$result = $this->sponsorship->edit_sponsor_info($id, $name, $address, $contactName, $contactNo);
			if ($result == FALSE) {
				$this->_load_common_header();
				$this->_load_common_navigation();
				$this->_load_common_sidebar(get_user_sidebar($this->userinfo['role'], 'sponsor'));
				$params = array(
					'role' => $this->userinfo['role'],
					'sponsor' => $sponsor,
					'editstatus' => 'failed'
				);
				$this->load->view('common/sponsor_edit', $params);
				$this->_load_common_footer();
			} else {
				$this->session->set_flashdata('sponsoredit', 'success');
				redirect('mahalum/sponsor/index', 'refresh');
				exit;
			}
		}
	}
	
	public function add() {
		$this->_get_userinfo();
		
		$this->form_validation->set_error_delimiters('<span class="error">', '</span>');
		if ($this->form_validation->run('add_sponsor_profile') == FALSE) {
			$this->_load_common_header();
			$this->_load_common_navigation();
			$this->_load_common_sidebar(get_user_sidebar($this->userinfo['role'], 'sponsor'));
			$params = array(
				'role' => $this->userinfo['role'],
				'addstatus' => ''
			);
			$this->load->view('common/sponsor_add', $params);
			$this->_load_common_footer();
		} else {
			$name = $this->input->post('name', TRUE);
			$address = $this->input->post('address', TRUE);
			$contactName = $this->input->post('contactName', TRUE);
			$contactNo = $this->input->post('contactNo', TRUE);
			$user = $this->input->post('user', TRUE);
			$username = $this->input->post('username', TRUE);
			$password = $this->input->post('password', TRUE);
			$result = $this->sponsorship->add_sponsor($name, $address, $contactName, $contactNo, $user, $username, $password);
			if ($result == FALSE) {
				$this->_load_common_header();
				$this->_load_common_navigation();
				$this->_load_common_sidebar(get_user_sidebar($this->userinfo['role'], 'sponsor'));
				$params = array(
					'role' => $this->userinfo['role'],
					'addstatus' => 'failed'
				);
				$this->load->view('common/sponsor_add', $params);
				$this->_load_common_footer();
			} else {
				$this->session->set_flashdata('sponsoradd', 'success');
				redirect('mahalum/sponsor/index', 'refresh');
				exit;
			}
		}
	}
	
	public function detail($id = NULL) {
		$this->_get_userinfo();
		if ($id == NULL) {
			redirect('mahalum/sponsor/index', 'location');
			exit;
		}
		$sponsor = $this->sponsorship->get_sponsor_info_by_id($id);
		if ($sponsor == NULL) {
			redirect('mahalum/sponsor/index', 'location');
			exit;
		}
		
		$this->_load_common_header();
		$this->_load_common_navigation();
		$this->_load_common_sidebar(get_user_sidebar($this->userinfo['role'], 'sponsor'));
		$params = array(
			'sponsor' => $sponsor,
			'users' => $this->sponsorship->get_sponsor_user($id)
		);
		$this->load->view('common/sponsor_detail', $params);
		$this->_load_common_footer();
	}
}